import { Vec3 } from 'cc';
import { BaseState } from './BaseState';
import { AIController } from '../AIController';

/**
 * AI追逐状态
 * 
 * 功能：
 * 1. 追踪目标对象
 * 2. 计算追逐路径
 * 3. 处理追逐策略
 * 4. 管理追逐行为
 * 
 * 特点：
 * 1. 智能路径规划
 * 2. 动态速度调整
 * 3. 预测目标移动
 * 4. 避障功能
 */
export class ChaseState extends BaseState {
    private updatePathInterval: number = 0.5;
    private pathTimer: number = 0;
    private maxChaseDistance: number = 20;
    private minDistance: number = 2;

    public enter(): void {
        this.updatePath();
    }

    public exit(): void {
        this.controller.stopMoving();
    }

    public update(dt: number): void {
        const target = this.controller.getTarget();
        if (!target) {
            this.controller.changeState('idle');
            return;
        }

        // 更新路径
        this.pathTimer += dt;
        if (this.pathTimer >= this.updatePathInterval) {
            this.pathTimer = 0;
            this.updatePath();
        }

        // 检查距离
        const distance = Vec3.distance(this.controller.getPosition(), target.position);
        
        if (distance > this.maxChaseDistance) {
            this.controller.changeState('idle');
            return;
        }

        if (distance <= this.minDistance) {
            this.controller.changeState('attack');
            return;
        }
    }

    private updatePath(): void {
        const target = this.controller.getTarget();
        if (!target) return;

        this.controller.moveTowards(target.position);
    }
} 